package com.punch.oauth.config;

import cn.hutool.core.util.ArrayUtil;
import com.punch.common.constant.SecurityConstants;
import com.punch.oauth.handler.OauthLogoutSuccessHandler;
import com.punch.oauth.mobile.MobileCodeAuthenticationSecurityConfig;
import com.punch.oauth.openid.OpenIdAuthenticationSecurityConfig;
import com.punch.oauth.service.impl.UaaUserServiceImpl;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.security.web.authentication.logout.LogoutHandler;

import javax.annotation.Resource;

/**
 * SpringSecurity配置
 * Created by macro on 2020/6/19.
 */
@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {


    @Resource
    private AuthenticationEntryPoint authenticationEntryPoint;

    @Resource
    private LogoutHandler oauthLogoutHandler;

    @Resource
    private UaaUserServiceImpl userDetailsService;

    //手机验证码获取token
    @Resource
    private MobileCodeAuthenticationSecurityConfig mobileCodeAuthenticationSecurityConfig;

    //openId获取token
    @Resource
    private OpenIdAuthenticationSecurityConfig openIdAuthenticationSecurityConfig;

    @Resource
    private IgnoreUrlsConfig ignoreUrlsConfig;

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests()
                .antMatchers(ArrayUtil.toArray(ignoreUrlsConfig.getUrls(), String.class)).permitAll()
                //授权服务器关闭basic认证
                .anyRequest().authenticated()
                .and()
                .logout()
                .logoutUrl(SecurityConstants.LOGOUT_URL)
                .logoutSuccessHandler(new OauthLogoutSuccessHandler())
                .addLogoutHandler(oauthLogoutHandler)
                .clearAuthentication(true)
                .and()
                .apply(mobileCodeAuthenticationSecurityConfig)
                .and()
                .apply(openIdAuthenticationSecurityConfig)
                .and()
                .csrf().disable()
                // 解决不允许显示在iframe的问题
                .headers().frameOptions().disable().cacheControl();
//        http.formLogin()
//                //.loginPage(SecurityConstants.LOGIN_PAGE)
//                .loginProcessingUrl(SecurityConstants.OAUTH_LOGIN_PRO_URL)
//                .successHandler(authenticationSuccessHandler);
        if (authenticationEntryPoint != null) {
            http.exceptionHandling().authenticationEntryPoint(authenticationEntryPoint);
        }
        http.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS);
    }

    @Bean // password 模式需要此bean
    @Override
    public AuthenticationManager authenticationManagerBean() throws Exception {
        return super.authenticationManagerBean();
    }


    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(userDetailsService);
    }

}